import 'package:flutter/material.dart';

// ignore: must_be_immutable
class NumChangeWidget extends StatefulWidget {
  final double height;
  int num;
  final ValueChanged<int> onValueChanged;

  NumChangeWidget(
      {Key key, this.height = 36.0, this.num = 0, this.onValueChanged})
      : super(key: key);

  @override
  _NumChangeWidgetState createState() {
    return _NumChangeWidgetState();
  }
}

class _NumChangeWidgetState extends State<NumChangeWidget> {
  @override
  Widget build(BuildContext context) {
    return Container(
      height: widget.height,
      decoration: BoxDecoration(
        // borderRadius: BorderRadius.all(Radius.circular(2.0)),
        border: Border.all(color: Color(0xFF333333), width: 1),
      ),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          GestureDetector(
            onTap: _minusNum,
            child: Container(
              width: 40.0,
              color: Color(0xFFA8EDFF),
              alignment: Alignment.center,
              // child: Image.asset(AssetsImages.NUM_MINUS), // 设计图
              child: Icon(Icons.remove, size: 34),
            ),
          ),
          Container(
            width: 1,
            color: Color(0xFF333333),
          ),
          Container(
            width: 40.0,
            alignment: Alignment.center,
            child: Text(
              widget.num.toString(),
              maxLines: 1,
              style: TextStyle(
                  fontSize: 26.0,
                  color: Colors.black,
                  fontWeight: FontWeight.bold),
            ),
          ),
          Container(
            width: 1,
            color: Color(0xFF333333),
          ),
          GestureDetector(
            onTap: _addNum,
            child: Container(
              width: 40.0,
              color: Color(0xFFA8EDFF),
              alignment: Alignment.center,
              // child: Image.asset(AssetsImages.NUM_ADD), // 设计图
              child: Icon(Icons.add, size: 34),
            ),
          ),
        ],
      ),
    );
  }

  void _minusNum() {
    if (widget.num == 0) {
      return;
    }

    setState(() {
      widget.num -= 1;

      if (widget.onValueChanged != null) {
        widget.onValueChanged(widget.num);
      }
    });
  }

  void _addNum() {
    setState(() {
      widget.num += 1;

      if (widget.onValueChanged != null) {
        widget.onValueChanged(widget.num);
      }
    });
  }
}
